package com.gz.logistics.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.library.pojo.OrderInfo;
import com.gz.logistics.library.pojo.ProductInfo;
import com.gz.logistics.library.util.Tools;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * author wanner1991
 * 主页订单适配器
 */
public class HomeOrderAdapter extends BaseAdapter {
    private static  final String TAG = HomeOrderAdapter.class.getSimpleName();
    private List<OrderInfo> mDataList;
    private Context mContext;
    private UpdataUI mUpdataUI;

    public HomeOrderAdapter(List<OrderInfo> mDataList, Context mContext, UpdataUI mUpdataUI) {
       // this.mDataList = new ArrayList<OrderInfo>(mDataList);
        this.mDataList = mDataList;
        this.mContext = mContext;
        this.mUpdataUI = mUpdataUI;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.home_orders_item,null);
            holder = new ViewHolder();
            holder.btn_product_selected = (ImageView) view.findViewById(R.id.btn_product_selected);
            holder.tv_order_number = (TextView) view.findViewById(R.id.tv_order_number);
            holder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            holder.tv_receiver_name = (TextView) view.findViewById(R.id.tv_receiver_name);
            holder.tv_receiver_phone = (TextView) view.findViewById(R.id.tv_receiver_phone);
            holder.tv_receiver_address = (TextView) view.findViewById(R.id.tv_receiver_address);
            holder.listProducts = (LinearLayout) view.findViewById(R.id.list_products);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        final  OrderInfo info  = mDataList.get(i);
        // 初始化选中按钮
        if (info.isSelected()){
            holder.btn_product_selected.setBackgroundResource(R.mipmap.ic_shopping_selected);
        }else{
            holder.btn_product_selected.setBackgroundResource(R.mipmap.ic_shopping_unselected);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        holder.tv_order_number.setText("订单编号："+info.getSn());
        holder.tv_receiver_name.setText("收货人姓名："+info.getReceiver_name());
        holder.tv_receiver_phone.setText("收货人电话："+info.getReceiver_tel());
        holder.tv_receiver_address.setText("收货人地址："+info.getReceiver_add());
        holder.listProducts.removeAllViews();


        // 设置选中按钮
        holder.btn_product_selected.setTag(info);
        holder.btn_product_selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderInfo info2 = (OrderInfo) view.getTag();
                if(info2.isSelected()){
                    if (info2.getSn() == info.getSn()){
                        info2.setIsSelected(false);
                        view.setBackgroundResource(R.mipmap.ic_shopping_unselected);
                    }
                }else{
                    if (info2.getSn() == info.getSn()){
                        info2.setIsSelected(true);
                        view.setBackgroundResource(R.mipmap.ic_shopping_selected);
                    }
                }
                mUpdataUI.updata();
            }
        });

        return view;
    }
    // 当对订单条目里的选中按钮进行操作时，主界面实现该接口，用来更新ui ，如果把所有的条目都置为选中，则把首页上面的全选按钮置为选中状态
    public interface  UpdataUI{
        void updata();
    }
    private class ViewHolder{
        ImageView btn_product_selected;
        TextView tv_order_number;
        TextView tv_create_time;
        TextView tv_receiver_name;
        TextView tv_receiver_phone;
        TextView tv_receiver_address;
        LinearLayout listProducts;
    }
}
